<template>
  <div>
    <Search :search="search" :reset="reset">
      <template v-slot:body>
        <el-form-item label="组织" prop="org_code">
          <!-- <el-input v-model="query.org_code" name="org_code" clearable /> -->
          <el-select v-model="query.org_code" name="org_code" placeholder="请选择组织" clearable>
            <el-option :label="item.label" :value="item.value" v-for="item in orgList" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="仓库" prop="to_wms">
          <el-select v-model="query.to_wms" name="to_wms" placeholder="请选择仓库" clearable filterable >
            <el-option :label="item.label" :value="item.value" v-for="item in warehouse" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="变动类型" prop="trans_type">
          <el-select v-model="query.trans_type" name="trans_type" placeholder="请选择变动类型" clearable>
            <el-option :label="item.label" :value="item.value" v-for="item in InvTransList" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="单据类型" prop="order_type">
          <el-select v-model="query.doc_type" name="doc_type" placeholder="请选择单据类型" clearable>
            <el-option :label="item.label" :value="item.value" v-for="item in InvTransDocList" :key="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="单据" prop="docno">
          <el-input v-model="query.docno" name="docno" clearable />
        </el-form-item>
        <el-form-item label="品号" prop="material_no">
          <el-input v-model="query.material_no" name="material_no" clearable />
        </el-form-item>
        <!-- <el-form-item label="原始箱号" prop="from_lotnum">
          <el-input v-model="query.from_lotnum" name="from_lotnum" clearable />
        </el-form-item> -->
        <el-form-item label="箱号" prop="to_lotnum">
          <el-input v-model="query.to_lotnum" name="to_lotnum" clearable />
        </el-form-item>

      </template>
    </Search>
    <div class="table-default">
      <el-table :data="tableData" v-loading="loading" border>
        <el-table-column prop="org_name" label="组织" width="100" />
        <el-table-column prop="cargo_name" label="货主" width="100" />
        <el-table-column prop="docno" label="单据编号" width="200" />
        <el-table-column prop="doc_type" label="单据类型" width="120" />
        <el-table-column prop="trans_type" label="变动类型" width="90" />
        <el-table-column prop="material_no" label="品号" width="120" />
        <el-table-column prop="material.fname" label="品名" width="200" />
        <el-table-column prop="material.funit_name" label="单位" width="60" />
        <!-- <el-table-column prop="from_wms" label="原始仓库代码" width="120" />
        <el-table-column prop="from_wmss.fname" label="原始仓库名称" width="120" />
        <el-table-column prop="from_location" label="原始库位代码" width="120" />
        <el-table-column prop="from_locations.repositoryname" label="原始库位名称" width="120" />
          <el-table-column prop="from_lotnum" label="原始箱号" width="180" /> -->
        <el-table-column prop="to_wms" label="仓库代码" width="100" />
        <el-table-column prop="to_wmss.fname" label="仓库名称" width="140" />
        <el-table-column prop="to_location" label="库位代码" width="100" />
        <el-table-column prop="to_locations.repositoryname" label="库位名称" width="100" />
        <el-table-column prop="to_lotnum" label="箱号" width="180" />
        <el-table-column prop="supplier_code" label="来源单号" width="180" />
        <el-table-column prop="start_qty" label="期初" width="90"
          ><template #default="scope">
            <span v-if="Number(scope.row.start_qty) == 0"></span>
            <span v-else>{{ Number(scope.row.start_qty) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="qty" label="变动数量" width="100"
          ><template #default="scope">
            <span v-if="Number(scope.row.qty) == 0"></span>
            <span v-else>{{ Number(scope.row.qty) }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="end_qty" label="期末" width="90"
          ><template #default="scope">
            <span v-if="Number(scope.row.end_qty) == 0"></span>
            <span v-else>{{ Number(scope.row.end_qty) }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="created_at" label="变动时间" width="180" />
        <el-table-column prop="creator" label="创建者" width="180" />
      </el-table>
      <Paginate />
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import Create from './create.vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import { setTableHeight } from '/admin/utils/setHeight.js'
import http from '/admin/support/http'

const api = 'inventory/trans'

const { data, query, search, reset, loading } = useGetList(api)
const { destroy, deleted } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

const orgList = ref([])
const warehouse = ref([])
const InvTransList = ref([])
const InvTransDocList = ref([])

const getStatus = () => {
  http.get(`options/Org`).then(r => {
    console.log('组织数据', r.data)
    orgList.value = r.data.data
  })
  http.get(`options/InvTransType`).then(r => {
    console.log('变动类型', r.data)
    InvTransList.value = r.data.data
  })
  http.get(`options/InvTransDocType`).then(r => {
    console.log('单据类型', r.data)
    InvTransDocList.value = r.data.data
  })
  http.get(`warehouse/datatype`).then(r => {
    console.log('仓库数据', r.data)
    console.log('仓库数据',warehouse.value);

    warehouse.value = r.data.data.map(item=>{
      return {
        label:item.fname,
        value:item.fnumber
      }
    })

    
  })
}

getStatus()

onMounted(() => {
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>

<style lang="css">
.el-table {
  height: 87%;
}
</style>
